package sy.service.impl;

import org.springframework.jdbc.core.RowCallbackHandler;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import sy.pageModel.DataGrid;
import sy.pageModel.Secondary;
import sy.service.SecondaryServiceI;
import sy.util.StringUtil;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2016-07-13.
 */
@Service
public class SecondarySecondaryImpl extends BaseService implements SecondaryServiceI {
    @Override
    public DataGrid findSecondaryDataGird(Secondary service) {
        DataGrid dg = new DataGrid();
        String sql = "select count(*) from secondary_card s where 1=1   ";
        sql = addWhere(service, sql);//模糊查询
        long total = template.queryForLong(sql);
        dg.setTotal(total);//获取总数
        sql = "SELECT * from secondary_card s where 1=1";
        sql = addWhere(service, sql);
        sql = addOrderBy(service, sql);
        sql = addLimit(service, sql);
        final List<Secondary> secondaryList = new ArrayList<Secondary>();
        template.query(sql, new RowCallbackHandler(){
            public void processRow(ResultSet rs) throws SQLException {
                Secondary sd=new Secondary();
                sd.setId(rs.getString("id"));
                sd.setName(rs.getString("name"));
                sd.setCount(rs.getInt("counts"));
                sd.setTypeId(rs.getString("typeid"));
                sd.setMemberFee(rs.getString("reg_money"));
                sd.setMinRegCharge(rs.getString("min_money"));
                secondaryList.add(sd);
            }});
        dg.setRows(secondaryList);
        return dg;
    }

    @Override
    @Transactional
    public int addServiceAction(Secondary service) {
        String sql="INSERT INTO secondary_card(id, counts, typeid, reg_money, name, min_money, sum_money) VALUES (?,?,?,?,?,?,?)";
        int rows=template.update(sql,new Object[]{StringUtil.getId(),service.getCount(),service.getTypeId(),service.getMemberFee(),service.getTypeName(),service.getMinRegCharge(),100000000});
        return rows;
    }

    @Override
    @Transactional
    public int editServiceAction(Secondary service) {
        String sql="update secondary_card set counts=?,typeid=?,reg_money=?,name=?,min_money=?  where id=? ;";
        int rows=template.update(sql,new Object[]{service.getCount(),service.getTypeId(),service.getMemberFee(),service.getTypeName(),service.getMinRegCharge(),service.getId()});
        return rows;
    }

    @Override
    @Transactional
    public int deleteServiceAction(Secondary service) {
        String sql="DELETE FROM secondary_card WHERE id=?";
        int rows=0;
        String ids[]=service.getIds().split(",");
        for (int i = 0; i <ids.length ; i++) {
            rows=template.update(sql,new Object[]{ids[i]});
        }
        return rows;
    }


    private String addLimit(Secondary curr, String sql) {
        int fromRow = (curr.getPage() - 1) * curr.getRows();
        sql += " limit " + fromRow + "," + curr.getRows();
        return sql;
    }

    private String addWhere(Secondary curr, String sql) {
//        if(!StringUtil.isNullOrEmpty(curr.getName())) sql += " and s.codes like '%" + curr.getName() + "%'";
//        if(!StringUtil.isNullOrEmpty(curr.getIsShow())) sql += " and s.is_show_title = '" + curr.getIsShow() + "'";
        return sql;
    }


    private String addOrderBy(Secondary curr, String sql) {
        if (curr.getSort() != null) {
//            if("name".equals(curr.getSort())) curr.setSort("codes");
            sql += " order by " + curr.getSort() + " " + curr.getOrder();
        }
        return sql;
    }
}
